﻿var K;
var dicloadfs = {};
var dicloadffn = {};
jQuery.ploadFile = function (sfTp, fnCallBack) {
    function seteditoropt() {
        K = KindEditor;
//        K.DEBUG = true;
        K.basePath = K.options.basePath = '/jqPlugin/kindeditor/';
        K.options.themesPath = K.options.basePath + 'themes/';
        K.options.langPath = K.options.basePath + 'lang/';
        K.options.pluginsPath = K.options.basePath + 'plugins/';
        typeof (fnCallBack) == "function" && fnCallBack();
    };
    function _addcmpfn() {
        if (typeof (fnCallBack) == "function") {
            var _arrfn = dicloadffn[sfTp] || new Array();
            if (sfTp == "kindeditor") {
                _arrfn.push(seteditoropt);
            } else {
                _arrfn.push(fnCallBack);
            }
            if (dicloadffn[sfTp] == undefined || dicloadffn[sfTp] == null) {
                dicloadffn[sfTp] = _arrfn;
            }
        }
    };
    if (dicloadfs[sfTp] != undefined && dicloadfs[sfTp] != null) {
        if (dicloadfs[sfTp] == "2") {
            typeof (fnCallBack) == "function" && fnCallBack();
            return;
        }
        else if (dicloadfs[sfTp] == "1") {
            _addcmpfn();
            return;
        }
    }
    dicloadfs[sfTp] = '1';
    _addcmpfn();
    function _cmpfn() {
        dicloadfs[sfTp] = '2';
        var _rrfn = dicloadffn[sfTp];
        if (_rrfn != undefined && _rrfn != null) {
            $.each(_rrfn, function (key, val) {
                typeof (val) == "function" && val();
            });
        }
    };
    if (sfTp == "Tip") {
        jQuery.loadScript("/jqPlugin/poshytip/jquery.poshytip.min.js").done(_cmpfn);
        jQuery.loadCss("/jqPlugin/poshytip/tip-yellowsimple/tip-yellowsimple.css");
        jQuery.loadCss("/jqPlugin/poshytip/tip-yellowsimple/tip-yellow.css");
    }
    else if (sfTp == "Grid") {
        jQuery.loadScript("/??jqPlugin/sigma_grid/gt_grid_all.js,jqPlugin/sigma_grid/gtBaseJs.js").done(_cmpfn);
        jQuery.loadCss("/jqPlugin/sigma_grid/gt_grid.css");
    }
    else if (sfTp == "Bought") {
        jQuery.loadScript("/themes/Style1/jsBought.js" ).done(_cmpfn);
    }
    else if (sfTp == "layer") {
        if (typeof(layer)!="undefined") {
            _cmpfn();
        } else {
            jQuery.loadScript("/jqPlugin/layer/layer.min.js").done(_cmpfn);
        }
    }
    else if (sfTp == "laytpl") {
        if (typeof(laytpl)!="undefined") {
            _cmpfn();
        } else {
            jQuery.loadScript("/jqPlugin/js/laytpl.js").done(_cmpfn);
        }
    }
    else if (sfTp == "uploadify") {
        jQuery.loadScript("/jqPlugin/uploadify/jquery.uploadify.min.js").done(_cmpfn);
        jQuery.loadCss("/jqPlugin/uploadify/uploadify.css");
    }
    else if (sfTp == "kindeditor") {
        jQuery.loadScript("/??jqPlugin/kindeditor/kindeditor.js,jqPlugin/kindeditor/lang/zh_CN.js").done(seteditoropt);
        jQuery.loadCss("/jqPlugin/kindeditor/themes/default/default.css");
    }
    else if (sfTp == "prbar") {
        jQuery.loadScript("/themes/Style1/prbar.js" + '?t=' + new Date().getTime()).done(_cmpfn);
    }
};
jQuery.getValidateImg = function (cimgid) {
    $("#" + cimgid).html('').append($('<img  align="top" src="/pgajax.axd?VDC=1&PRAD=' + sCurrRadom + '&t=' + new Date().getTime() + '"  />').click(function () { jQuery.getValidateImg(cimgid); }));
};
jQuery.ValidateImgCode = function (inpcode, fnCallBack) {
    jQuery.loadHtml('/pgajax.axd?VDC=2&INPVDCD=' + inpcode + '&PRAD=' + sCurrRadom + '&t=' + new Date().getTime()).done(fnCallBack);
};
jQuery.setLoginOut = function () {
    jQuery.loadHtml('/pgajax.axd?LOGINOUT=1').done(function () { window.location.reload(true) ; });
};
jQuery.setheadLoginInfo = function () {
    jQuery.loadHtml('/pgajax.axd?GETLOGIN=1').done(function (_rets) { if (_rets) { $(".sn-login-info").html(_rets); } });
};
jQuery.loginCallBack = function (fnCallBack, isOpenLogin) {
    jQuery.loadHtml('/pgajax.axd?GETLOGIN_ST=1').done(function (_rets) { if (_rets != "0" && _rets != "") { fnCallBack(); } else if (isOpenLogin) { jQuery.showLogin(fnCallBack); } });
};
var _fndoLoginOk = null;
function doLoginOk() {
    if (_fndoLoginOk != null) {
        _fndoLoginOk();
        _fndoLoginOk = null;
    }
    jQuery.setheadLoginInfo();
};
jQuery.showLogin = function (fnCallBack) {
    _fndoLoginOk = fnCallBack;
    jQuery.ploadFile("layer", function (script, textStatus) {
        $.layer({
            type: 2,
            shade: [0.5, '#000'],
            fix: true,
            title: '请输入登录信息',
            maxmin: false,
            iframe: { src: '/c_LoginIFrame.htm' },
            area: ['450px', '330px'],
            end: function (index) { }
        });
    });
};
var _uploadbtn=[];
jQuery.setUploadFile = function (_dombtnId, _extPara, fnCallBack) {
    if (!_dombtnId) { return; }
    var _dombtn = ($("#" + _dombtnId).length > 0 ? $("#" + _dombtnId)[0] : $("<input type='button' style='display:none;' id='" + _dombtnId + "' value='' />").appendTo("body")[0]);
    if (_uploadbtn[_dombtnId]) { _uploadbtn[_dombtnId].remove(); _uploadbtn[_dombtnId] = null; }
    _uploadbtn[_dombtnId] = K.uploadbutton({
        button: _dombtn,
        url: '/pgajax.axd?F_UPLOAD=1',
        extraParams: _extPara || { fType: '', FileId: '', RelId: '', rType1: '0', rType2: '',blSave:'0' },
        afterUpload: function (data) {
            fnCallBack(data);
        },
        afterError: function (str) {
            alert('自定义错误信息: ' + str);
        }
    });
    _uploadbtn[_dombtnId].fileBox.change(function (e) {
        _uploadbtn[_dombtnId].submit();
    });
    //    $(_uploadbtn.fileBox).trigger("click");
};

function createPaging(_pagedivid, _page, _total, _func) {
    var $pagediv = $("#" + _pagedivid);
    if ($pagediv.find("input").attr("max") == _total) {
        var $page = $pagediv.find(".ep-pages a[p-num='" + _page + "']");
        if ($page && $page.length > 0 && ($page.next().length <= 0 || $page.next().length > 0 && !$page.next().hasClass("next-ctrl")) && ($page.prev().length <= 0 || $page.prev().length > 0 && !$page.prev().hasClass("prev-ctrl"))) {
            $page.removeClass("current").addClass("current");
            $page.prevAll().removeClass("current");
            $page.nextAll().removeClass("current");
            return;
        } 
    }
    if ($pagediv.find(".ep-pages").length <= 0) {
        $("#" + _pagedivid + " .ep-pages a").live("click", function () {
            if (!$(this).hasClass("current") && $(this).attr("p-num") != undefined && $(this).attr("p-num") != '') {
                _func($(this).attr("p-num"));
            } else if ($(this).hasClass("goto-page")) {
                var $topage = $(this).prev().find("input");
                if (parseInt($topage.val()) > parseInt($topage.attr('max'))) {
                    _func($topage.attr('max'));
                } else {
                    _func($topage.val());
                }
            }
        });
    }
    //create
    var _shtml = '<div class="ep-pages">';
    if (_page - 3 > 1) {
        _shtml += '<a href="javascript:void(0);" target="_self" p-num="1">首页</a><span class="ep-pages-ctrl ep-pages-e5e5e5">¨</span><a href="javascript:void(0);" target="_self" class="prev-ctrl" p-num="' + (_page - 1) + '">&lt;</a>';
    } else {
        _shtml += '<span class="ep-pages-e5e5e5">首页</span> <span class="ep-pages-ctrl ep-pages-e5e5e5">&lt;</span>';
    }
    for (var i = _page - 3; i <= _total; i++) {
        if (i <= 0) { continue; }
        _shtml += ' <a href="javascript:void(0);" ' + (i == _page ? 'class="current"' : '') + ' target="_self" p-num="' + i + '">' + i + '</a>';
        if (i >= _page + 3) { break; }
    }
    if (_page + 3 < _total) {
        _shtml += ' <a href="javascript:void(0);" target="_self" class="next-ctrl" p-num="' + (_page + 1) + '">&gt;</a><span class="ep-pages-ctrl ep-pages-e5e5e5">¨</span> <a href="javascript:void(0);" target="_self" p-num="' + _total + '">尾页</a>';
    } else {
        _shtml += ' <span class="ep-pages-ctrl ep-pages-e5e5e5">&gt;</span> <span class="ep-pages-e5e5e5">尾页</span>';
    }
    _shtml += ' <span style="border:0px; padding-top:2px;">共' + _total + '页，到第 <input class="input" type="number" value="1" min="1" max="' + _total + '"> 页</span><a href="javascript:void(0);" target="_self" class="goto-page">确定</a>';
    $pagediv.empty().html(_shtml);
};
function loadTagSuggest(_sgid) {
    var gids = _sgid || "";
    for (var i = 0; i < 10; i++) {
        if (!LS.get("CURR_LOOK_ID_" + i)) {
            continue;
        }
        if (LS.get("CURR_LOOK_IMG_" + i) == "#" || LS.get("CURR_LOOK_ID_" + i) == null) { continue; }
        gids += gids == "" ? LS.get("CURR_LOOK_ID_" + i) : "," + LS.get("CURR_LOOK_ID_" + i);
    }
    jQuery.loadHtml('/pgajax.axd?GET_TAGSUGGEST_JSON=1&G_IDS=' + gids).done(function (_sRtjson) {
        var _data = $.parseJSON(_sRtjson);
        var _htmltm = '';
        for (var i = 0; i < _data.rows.length; i++) {
            _htmltm += '<li ><a href="/c_GoodsSearch.htm?r=' + _data.rows[i].LABEL_NME + '" title="' + _data.rows[i].LABEL_NME + '">' + _data.rows[i].LABEL_NME + '</a></li>';
        }
        $(".tmTagSuggest").html(_htmltm); 
    });
};